package com.ly.lyyudao.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.lyyudao.domain.pojo.Users;
import com.ly.lyyudao.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 从数据库获取用户信息
 * 并封装成UserDetails对象
 * @auth wzy
 * @date 2024/4/22 15:45
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UsersService usersService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        // 根据用户名去数据库查询用户
        Users sysUser = usersService.getOne(queryWrapper);

        // 用户不存在
        if (sysUser == null) {
            throw new UsernameNotFoundException(username);
        }

        return new LoginUser(sysUser,null);
    }
}
